package com.rh.bmw4s.ui;

import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.rh.bmw4s.R;
import com.rh.bmw4s.bean.EventInfo;
import com.rh.bmw4s.utills.DialogUtils;
import com.rh.bmw4s.utills.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WebShowActivity extends BaseActivity {

    @BindView(R.id.webview)
    WebView webview;
    @BindView(R.id.webview_fail_back)
    ImageView wbBack;
    private String url;
    private RelativeLayout ll_fail = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_show);
        ButterKnife.bind(this);
        EventBus.getDefault().register(WebShowActivity.this);
        url = getIntent().getStringExtra("url");
        initView();
    }

    private void initView() {
        DialogUtils.showProgressDlg(WebShowActivity.this, "加载中...");
        ll_fail = (RelativeLayout) findViewById(R.id.ll_load_fail);
        WebSettings setting = webview.getSettings();
        setting.setJavaScriptEnabled(true);
        webview.addJavascriptInterface(new JSHook(), "android");

        setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        setting.setUseWideViewPort(true);
        setting.setLoadWithOverviewMode(true);

        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                DialogUtils.stopProgressDlg();
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                DialogUtils.stopProgressDlg();
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                DialogUtils.stopProgressDlg();
            }

            @Override
            public void onReceivedError(WebView webView, int i, String s, String s1) {
                super.onReceivedError(webView, i, s, s1);
                webview.setVisibility(View.GONE);
                ll_fail.setVisibility(View.VISIBLE);
                DialogUtils.stopProgressDlg();
            }

//            @Override
//            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
//                super.onReceivedHttpError(view, request, errorResponse);
//                webview.setVisibility(View.GONE);
//                ll_fail.setVisibility(View.VISIBLE);
//                DialogUtils.stopProgressDlg();
//            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();  // 接受所有网站的证书
                super.onReceivedSslError(view, handler, error);
            }

        });
        webview.loadUrl(url);
        //webview.loadUrl("javascript:cc('" + obj + "')");
    }

    @OnClick(R.id.webview_fail_back)
    public void onViewClicked() {
        WebShowActivity.this.finish();
    }

    //开放接口类
    public class JSHook extends Object {
        @JavascriptInterface
        public void back() {
            WebShowActivity.this.finish();
            //  startActivityForResult(new Intent(WebShowActivity.this, TestActivity.class), 111);
        }

        @JavascriptInterface
        public void tosearch() {
            Intent intent = new Intent(WebShowActivity.this, SearchActivity.class);
            intent.putExtra("searchtype", 1);
            WebShowActivity.this.startActivity(intent);
        }

        @JavascriptInterface
        public void addCar() {
            //添加车辆信息
            Intent intent = new Intent(WebShowActivity.this, AddCarActivity.class);
            intent.putExtra("isedit", 0);
            WebShowActivity.this.startActivity(intent);
        }

        @JavascriptInterface
        public void editCar(int arg) {
            //编辑车辆信息
            Intent intent = new Intent(WebShowActivity.this, AddCarActivity.class);
            intent.putExtra("mycarid", arg);
            intent.putExtra("isedit", 1);
            WebShowActivity.this.startActivity(intent);
        }

        @JavascriptInterface
        public void selectCar() {
            Intent intent = new Intent(WebShowActivity.this, SelectCarTypeActivity.class);
            WebShowActivity.this.startActivity(intent);

            // WebShowActivity.this.finish();
        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 111 && resultCode == 112) {
            String back = data.getExtras().getString("back");
            ToastUtils.toastForShort(WebShowActivity.this, back);
            webview.reload();
            webview.loadUrl("http://www.baidu.com");
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //回收webview加载的界面
        if (webview != null) {
            webview.destroy();
        }
        EventBus.getDefault().removeAllStickyEvents();
        EventBus.getDefault().unregister(WebShowActivity.this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void restartThis(EventInfo event) {
        if (event.getNum() == 11) {
            WebShowActivity.this.finish();
        }
    }


}
